package com.project.solomon.SessionBean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.project.entity.Property;
import com.project.entity.Property_Image;

/**
 * Session Bean implementation class PropForRentBean
 */
@Stateless
@LocalBean
public class PropForRentBean implements PropForRentBeanRemote {

	@PersistenceContext
	EntityManager em;
	
	public static final String RemoteJNDIName = PropForRentBean.class.getSimpleName() + "/remote";
    public static final String LocalJNDIName = PropForRentBean.class.getSimpleName() + "/local";
    
    public PropForRentBean() {
        // TODO Auto-generated constructor stub
    }
	
	@Override
	public void addNewPropForRent(Property prop) {
		// TODO Auto-generated method stub
		em.persist(prop);
	}

	@Override
	public String getNewID() {
		// TODO Auto-generated method stub
		String id = "";
		Query query = em.createQuery("SELECT pro.propID FROM PropForRent pro");
		@SuppressWarnings("unchecked")
		ArrayList<String> result = (ArrayList<String>) query.getResultList();
		
		int count = 0;
		for (int i = 0; i < result.size(); i++) {
			count++;
		}
		
		id = "P-" + String.format("%04d", count); 
		return id;
	}

	@Override
	public void addNewPropForRentImages(Property prop,
			List<Property_Image> Property_ImageList) {
		Property property = em.find(Property.class, prop.getPropID());
		Iterator ite = Property_ImageList.iterator();
		int i = 0;
		while(ite.hasNext())
		{
			Property_Image temp_img = (Property_Image)ite.next();
			property.getPropertyImages().add(temp_img);
			i++;
		}
		
	}

}
